package com.yu.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.crm.domain.pojo.CrmAreaNodeInfo;
import com.yu.crm.domain.pojo.CrmDevice;
import com.yu.crm.domain.request.AreaImageReq;
import com.yu.crm.domain.request.Punctuation;
import com.yu.crm.domain.response.RegionCameraRep;
import com.yu.crm.service.impl.CrmAreaNodeInfoService;
import com.yu.crm.service.impl.CrmDeviceService;
import com.yxzz.common.helper.valid.ValidatorHelpers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * /**
 * <p>
 *  开门公共服务
 * </p>
 *
 * @author Yang
 * @since 2019-12-18
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("/crm-device")
public class CrmDeviceController {
    @Autowired
    CrmDeviceService crmDeviceService;

    /**
     * 设备状态
     */
    @GetMapping(value = "/device-status")
    @ApiOperation(value = "设备状态")
    public void deviceStatus(String deviceIndexCode) {
        crmDeviceService.deviceStatus(deviceIndexCode);
    }

    @ApiOperation(value = "查询设备序列号")
    @GetMapping(value = "/deviceCodeByType")
    public List<String> getDeviceCode(String deviceName){
        return crmDeviceService.getDeviceCodeByType(deviceName);
    }

    @ApiOperation(value = "查询设备类型名")
    @GetMapping(value = "/deviceTypeName")
    public List<String> getDeviceTypeName(){
        return crmDeviceService.getDeviceTypeName();
    }

    /**
     * 新增或修改设备信息
     * @param crmDevice
     */
    @ApiOperation(value = "新增或修改设备信息")
    @PostMapping(value = "/add-org-update")
    public void addOrUpdate(@RequestBody CrmDevice crmDevice){
        crmDeviceService.addOrUpdate(crmDevice);
    }


    @ApiOperation(value = "通过设备平台类型和设备类型查询列表")
    @GetMapping(value = "/list-by-type")
    public RegionCameraRep listByPlatformTypeDeviceType(String platformType, String deviceType){
//        ValidatorHelpers.notEmpty(id, "设备编号不能为空");
        return crmDeviceService.listByPlatformTypeDeviceType(platformType,deviceType);
    }


    /**
     * 通过设备主键查询设备详情
     * @param id 设备主键
     * @return
     */
    @ApiOperation(value = "通过设备主键查询设备详情")
    @GetMapping(value = "/info-by-id")
    public CrmDevice infoById(@RequestParam("id") Long id){
        ValidatorHelpers.notEmpty(id, "设备编号不能为空");
        return crmDeviceService.infoById(id);
    }

    /**
     * 通过设备名查询设备信息
     * @param page
     * @param deviceName
     * @return
     */
//    @ApiOperation(value = "通过设备名查询设备信息")
//    @GetMapping(value = "/selectByName")
//    public IPage<CrmDevice> selectByName(Page<CrmDevice> page, String deviceName){
//        QueryWrapper<CrmDevice> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("is_deleted",0);
//        queryWrapper.eq("device_name",deviceName);
//        return crmDeviceService.page(page, queryWrapper);
//    }

    /**
     * 通过条件查询分页设备信息
     * @param page
     * @param condition 查询条件 (模糊查询  控制器名称，位置，ip地址，端口号，设备编号 )
     * @return
     */
    @ApiOperation(value = "通过条件查询分页设备信息")
    @GetMapping(value = "/page")
    public IPage<CrmDevice> page(Page<CrmDevice> page,
                                 @RequestParam(required = false) String deviceName,
                                 @RequestParam(required = false) String condition){
        return crmDeviceService.page(page, deviceName, condition);
    }

//    /**
//     * 批量开启/关闭设备
//     * @param map
//     *      批量修改  type 1 开启  2 关闭
//     *      { "ids": "[1,2,3]", "type": 1 }
//     */
//    @ApiOperation(value = "批量开启/关闭设备")
//    @PostMapping(value = "/update-list")
//    public void updateList(@RequestBody HashMap<String,Object> map){
//        ValidatorHelpers.notEmpty(map.get("ids"), "设备编号不能为空");
//        ValidatorHelpers.notEmpty(map.get("type"), "操作类型不能为空");
//        crmDeviceService.updateList(map);
//    }

    @ApiOperation("根据id批量删除")
    @DeleteMapping("/remove-batch-by-id")
    public void removeBatchById(@RequestParam("ids") String ids){
        crmDeviceService.deleteByIds(ids);
    }

    /**
     * 通过楼梯id软删除设备信息
     * @param id 设备编号
     */
    @ApiOperation(value = "通过楼梯id软删除设备信息")
    @GetMapping(value = "/delete")
    public void deleteById(@RequestParam Long id){
        ValidatorHelpers.notEmpty(id, "设备编号不能为空");
        crmDeviceService.deleteById(id);
    }

//    /**
//     * 通过设备编号扫码开门
//     * @param deviceSequence 设备编号
//     * @return
//     */
//    @ApiOperation(value = "通过设备编号扫码开门")
//    @GetMapping(value = "/info-by-device")
//    public void infoByDeviceSequence(@Param("deviceSequence") String deviceSequence){
//        ValidatorHelpers.notEmpty(deviceSequence, "设备编号不能为空,请重新扫码");
//        crmDeviceService.infoByDeviceSequence(deviceSequence);
//    }

//    /**
//     * 通过唯一标识实现开门
//     * @param cameraNumber
//     */
//    @ApiOperation(value = "通过唯一标识实现开门")
//    @PostMapping(value = "/open-device")
//    public void  openDevice(@Param("cameraNumber") String cameraNumber){
//        System.out.println("============open-device>>"+cameraNumber);
//        //传递一个唯一表示来进行开门操作
//        ValidatorHelpers.notEmpty(cameraNumber,"摄像头标识不能为空");
//        crmDeviceService.openDevice(cameraNumber);
//    }

//    /**
//     * 重新加载设备(不需要重新加载，只要启动时执行这个，之后后面执行开门方法就可以)
//     */
//    @ApiOperation(value = "重新加载设备")
//    @PostMapping(value = "/init-device")
//    public void initDevice(){
//        crmDeviceService.initDevice();
//    }

//    /**
//     * 通过摄像头唯一标示设备详情
//     * @return
//     */
//    @GetMapping(value = "/info-by-camera")
//    public CrmDevice infoByCameraNumber(@RequestParam("cameraNumber") String cameraNumber){
//        ValidatorHelpers.notEmpty(cameraNumber, "设备类型不能为空");
//        return crmDeviceService.infoByCameraNumber(cameraNumber);
//    }

//    @ApiOperation(value = "根据标点附件的ID查找所有的摄像头类型的设备")
//    @PostMapping(value = "/get-devices-by-attachment-id/{attachmentId}")
//    public List<CrmDevice> getDevicesByAttachmentId(@PathVariable String attachmentId){
//        QueryWrapper<CrmDevice> wrapper = new QueryWrapper<>();
//        wrapper.eq("is_deleted",0);
//        wrapper.eq("attachment_id",attachmentId);
//        return crmDeviceService.list(wrapper);
//    }
}
